﻿using CommonTools.Util;
using NETMQLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Color = System.Drawing.Color;

namespace CommonTools.Desktop
{
    


    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ChartArea AREA = new ChartArea() { Name = "Line" };             // 画图区域
        private ChartArea AREA2 = new ChartArea() { Name = "Line2" };             // 画图区域
        private Series SERIES = new Series();                                   // 一条折线
        private Series SERIES2 = new Series();                                   // 一条折线
        private Legend LEGEND = new Legend();                                   // 一个图例
        private Legend LEGEND2 = new Legend();                                   // 一个图例
        private System.Timers.Timer timer = new System.Timers.Timer();          // 定时器
        private int INTERVAL = 10;                                             // 定时器时间
        private int MAXPLOT = 10000;
        List<string> list = new List<string>();



        WindowEmbedHelper embed = new WindowEmbedHelper();

        //ServerMQ server;
        ClientMQ client;
        Stopwatch sw = Stopwatch.StartNew();
        int clientCount = 0;
        int serverCount = 0;
        PubMQ pubMQ = null;
        SubMQ subMQall = null;
        SubMQ subMQAAA = null;
        SubMQ subMQBBB = null;
        public MainWindow()
        {
            InitializeComponent();

            //ServerClientMQTest();

            //P_SMQTest();

            //TCPTest();

            InprocTest();
        }

        private void InprocTest()
        {
            Test t = new Test();
            t.TestFunc();
        }

        private void TCPTest()
        {
            string path = @"D:\Recv\问界M7海外版项目 IVI_INFO CAN通信协议 V1.9.4 20230530_.dbc";
            TcpFileServerHelper tcpFileServerHelper = new TcpFileServerHelper("127.0.0.1", 29908, path);
            tcpFileServerHelper.ProgressRefush += TcpFileServerHelper_ProgressRefush; ;
        }

        private void TcpFileServerHelper_ProgressRefush(int obj)
        {
            Console.WriteLine(obj);
        }


        private void P_SMQTest()
        {
            Task.Factory.StartNew(() =>
            {
                pubMQ = new PubMQ("127.0.0.1", 22134);

            });

            Task.Factory.StartNew(() =>
            {
                subMQall = new SubMQ("127.0.0.1", 22134);
                subMQall.ReceiveFrameStringEvent += SubMQall_ReceiveFrameStringEvent;
                subMQAAA = new SubMQ("127.0.0.1", 22134, "AAA");
                subMQAAA.ReceiveFrameStringEvent += SubMQAAA_ReceiveFrameStringEvent;
                subMQBBB = new SubMQ("127.0.0.1", 22134, "BBB");
                subMQBBB.ReceiveFrameStringEvent += SubMQBBB_ReceiveFrameStringEvent;
            });
    

        }

        private void SubMQBBB_ReceiveFrameStringEvent(string obj)
        {
            bbbCount++;
            if (bbbCount % 10000 == 0)
                Console.WriteLine($"BBB : {bbbCount} - {sw.ElapsedMilliseconds}");
            //pubMQ.SendData("Test2", "BBB");
            //Console.WriteLine($"BBB : {obj}");
        }

        private void SubMQAAA_ReceiveFrameStringEvent(string obj)
        {
            aaaCount++;
            if (aaaCount % 10000 == 0)
                Console.WriteLine($"AAA : {aaaCount} - {sw.ElapsedMilliseconds}");
            //pubMQ.SendData("Test1", "AAA");
            //pubMQ.SendData("Test0");
            //Console.WriteLine($"AAA : {obj}");
        }

        int allCount = 0;
        int aaaCount = 0;
        int bbbCount = 0;
        private void SubMQall_ReceiveFrameStringEvent(string obj)
        {
            allCount++;
            if (allCount % 10000 == 0)
                Console.WriteLine($"all : {allCount} - {sw.ElapsedMilliseconds}");
            //pubMQ.SendData("Test0");
        }

        private void ServerClientMQTest()
        {
            //server = new ServerMQ("127.0.0.1", 7895);
            //server.ReceiveFrameStringEvent += Server_ReceiveFrameStringEvent;
            client = new ClientMQ("127.0.0.1", 7895);
            client.ReceiveFrameStringEvent += Client_ReceiveFrameStringEvent;
            client.SendData("0");
        }

        private void Client_ReceiveFrameStringEvent(string obj)
        {
            //if (int.TryParse(obj, out int val))
            //{
            //    val++;
            //    if (val % 100000 == 0) Console.WriteLine($"{val} - {sw.ElapsedMilliseconds}");
            //    client.SendData(val.ToString());
            //}
            //clientCount++;
            //if (clientCount % 100000 == 0) Console.WriteLine($"C:{clientCount} - {sw.ElapsedMilliseconds}");
            //server.SendData(clientCount.ToString());
        }

        private void Server_ReceiveFrameStringEvent(string obj)
        {
            //if (int.TryParse(obj, out int val))
            //{
            //    val++;
            //    if (val % 100000 == 0) Console.WriteLine($"{val} - {sw.ElapsedMilliseconds}");
            //    server.SendData(val.ToString());
            //}
            //serverCount++;
            //if (serverCount % 100000 == 0) Console.WriteLine($"S:{serverCount} - {sw.ElapsedMilliseconds}");
            //server.SendData(serverCount.ToString());
        }

        private void self_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            embed.ResizeWindow();
        }

        private void self_Loaded(object sender, RoutedEventArgs e)
        {
            //Task.Factory.StartNew(() =>
            //{
            //    while (true)
            //    {
            //        pubMQ.SendData("Test0");
            //        pubMQ.SendData("Test1", "AAA");
            //        pubMQ.SendData("Test2", "BBB");
            //    }
            //});
            //Task.Factory.StartNew(() =>
            //{
            //    while (true)
            //    {
            //        pubMQ.SendData("Test1", "AAA");
            //    }
            //});
            //Task.Factory.StartNew(() =>
            //{
            //    while (true)
            //    {
            //        pubMQ.SendData("Test2", "BBB");
            //    }
            //});
            //pubMQ.SendData("AAA");
            //pubMQ.SendData("Test3");
            //pubMQ.SendData("AAATest4");

            //System.Diagnostics.Process process = new System.Diagnostics.Process();
            //string arguments = "";
            //process.StartInfo = new System.Diagnostics.ProcessStartInfo(@"D:\Code\Work\can_tool_chain\WeCAN\WeCAN.DeskTop\bin\Debug\PHX Test Studio - CAN.exe", arguments);

            //process.StartInfo.RedirectStandardInput = true;
            //process.StartInfo.RedirectStandardOutput = true;
            //process.StartInfo.RedirectStandardError = true;
            //process.StartInfo.UseShellExecute = false;
            //process.Start();
            //while (process.MainWindowHandle == IntPtr.Zero)
            //{
            //    Thread.Sleep(100);
            //}
            //embed.SetParent(panel.Handle, process.MainWindowHandle);
            //embed.ResizeWindow();

            //embed.SetParent(panel.Handle, "DigitalTestAgent");
            ////embed.SetParent(panel.Handle, "PHX Test Studio - IVI");
            ////embed.SetParent(panel.Handle, "PHX Test Studio - IVI");
            //embed.ResizeWindow();

        }
    }
}
